package com.example.jegotrip.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.jegotrip.R;

import java.util.ArrayList;
import java.util.List;

public class LeftShopListAdapter extends RecyclerView.Adapter<LeftShopListAdapter.LeftShopViewHolder> {

    private List<String> dataList = new ArrayList<>();
    private int currentIndex = 0;

    public LeftShopListAdapter(List<String> dataList) {
        this.dataList = dataList;
    }

    @NonNull
    @Override
    public LeftShopViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.left_shop_list_item,null);
        return new LeftShopViewHolder(view);
    }

    @Override
    @SuppressLint("RecyclerView")
    public void onBindViewHolder(@NonNull LeftShopViewHolder holder, int position) {

        String name = dataList.get(position);
        holder.tv_name.setText(name);

//        一级分类点击
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mleftListOnClickItemListener != null){
                    mleftListOnClickItemListener.onItemClick(position);
                }
            }
        });

        if (currentIndex == position){
            holder.itemView.setBackgroundResource(R.drawable.type_selector_bg);
        } else {
            holder.itemView.setBackgroundResource(R.drawable.type_selector_normal);

        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    static class LeftShopViewHolder extends RecyclerView.ViewHolder{

        private TextView tv_name;
        public LeftShopViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
        }
    }

    public void setMleftListOnClickItemListener(LeftListOnClickItemListener mleftListOnClickItemListener) {
        this.mleftListOnClickItemListener = mleftListOnClickItemListener;
    }

    private LeftListOnClickItemListener mleftListOnClickItemListener;

    public interface LeftListOnClickItemListener {
        void onItemClick(int position);
    }

    public void setCurrentIndex(int position){
        this.currentIndex = position;
//        这句话不能少！！！
        notifyDataSetChanged();

    }
}
